﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.Models.SYS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.ServiceModel;
using System.Web.Mvc;

namespace KPIS.GERP.WCF.GIMS
{
	[ServiceContract]
	public interface IIcmMasLookUpService
	{
		[OperationContract]
		string GetLookupNameFromAllLookup(object lookupId);

		[OperationContract]
		IEnumerable<IcmMasLookUp> Select(IcmMasLookUp filter);

		[OperationContract]
		List<SelectListItem> MasLookUpSelectList(IcmMasLookUpType.LookUpType lookUpType);

        [OperationContract]
        string SelectNameMasLookUp(int MasLookUpsId);
	}

	[ServiceErrorBehavior(typeof (ElmahErrorHandler))]
    public class IcmMasLookUpService : IIcmMasLookUpService
	{
		public string GetLookupNameFromAllLookup(object lookupId)
		{
			using (GERPContext db = new GERPContext(false))
			{
				var allLookup = db.IcmMasLookUps.Where(o => o.RecordStatus == RecordStatus.UsedStatus);
				foreach (IcmMasLookUp lu in allLookup)
				{
					if (IsObjectToStringEqual(lookupId, lu.LookUpId))
						return lu.LookUpName;
				}
				return string.Empty;

			}
		}

        public bool IsObjectToStringEqual(object obj1, object obj2)
        {
            obj1 = MakeStringEmptyAndLower(obj1 == null ? string.Empty : obj1.ToString());
            obj2 = MakeStringEmptyAndLower(obj2 == null ? string.Empty : obj2.ToString());
            return obj1.Equals(obj2);
        }

        public static string MakeStringEmptyAndLower(string str)
        {
            return (str ?? string.Empty).ToLower().Trim();
        }

		public IEnumerable<IcmMasLookUp> Select(IcmMasLookUp filter)
		{
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<IcmMasLookUp> query = context.IcmMasLookUps;

                    if (filter != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(filter, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
		}

		public List<SelectListItem> MasLookUpSelectList(IcmMasLookUpType.LookUpType lookUpType)
		{
			using (GERPContext db = new GERPContext(false))
			{
				var data = new List<SelectListItem>();

				try
				{
					List<Models.ICM.IcmMasLookUp> listMasLookUp = (from m in db.IcmMasLookUps
					                                            where m.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
					                                                  && m.LookUpTypeId == (int) lookUpType
					                                            select m).ToList();

					foreach (var MasLookUp in listMasLookUp)
					{
						var item = new SelectListItem {Text = MasLookUp.LookUpName, Value = MasLookUp.LookUpId.ToString()};
						if (MasLookUp.LookUpId == (int) lookUpType) item.Selected = true;
						data.Add(item);
					}

				}
				catch (Exception ex)
				{
					throw ex;
				}

				return data;
			}
		}
        public string SelectNameMasLookUp(int MasLookUpsId)
        {
            using (GERPContext db = new GERPContext(false))
            try
            {
                string Tmp = null;
                if (MasLookUpsId != 0)
                {
                    string MasLookUpName = (from m in db.IcmMasLookUps
                                            where m.LookUpId == MasLookUpsId
                                            select m.LookUpName).First().ToString();
                    Tmp = MasLookUpName;
                }
                else
                {
                    Tmp = "";
                }
                return Tmp;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
	}
}